﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace _3DHouse
{
    /// <summary>
    /// NewFile.xaml 的交互逻辑
    /// </summary>
    public partial class NewFile : Window
    {
        public NewFile()
        {
            InitializeComponent();
            List<string> qianitems = new List<string>();
            List<string> fangitems = new List<string>();
            for (int i = 1; i <= 100; i++)
            {
                string num = i.ToString();
               num= num.PadLeft(13,'0');
               qianitems.Add(num);
                    num= i.ToString().PadLeft(4,'0');
                    fangitems.Add(num);
            }
            comboBox3.ItemsSource = fangitems;
            comboBox1.ItemsSource = qianitems;
            comboBox1.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
           // GetTools();
        }
        bool isEmpty = false;//是否是空模板
        /// <summary>
        /// 户型图编号
        /// </summary>
       public string houseNumber;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
           /* if (isEmpty)//空模板
            {
                Drawing xin = new Drawing();
                xin.Show();
            }
            else
            {
                if (!File.Exists(selectItem.Tag.ToString()))
                {
                    MessageBox.Show("模板文件不存在");
                    return;
                }
                Drawing xin = new Drawing(selectItem.Tag.ToString());
                xin.Show();
            }*/
            //生成变换
            string qianNum = comboBox1.SelectedItem.ToString();
            string fangNum = comboBox3.SelectedValue.ToString();
          string houseNum=  DateTime.Now.ToString("yyyyMMddHHmm");
          houseNumber = qianNum + "_" + fangNum + "_" + houseNum;
          //if (houseNumber == null || houseNumber.Equals(""))
          //{
              
          //}
          //else
          //{
          //    Drawing xin = new Drawing();
          //    xin.hmId = houseNumber;
          //    xin.Show();
          //}
            this.DialogResult = true;
            this.Close();
            

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        void GetTools()
        {
            string path = Directory.GetCurrentDirectory() + "\\template";
            if (!Directory.Exists(path))
            {
                MessageBox.Show("tools不存在");
                return;
            }
            //获得文件夹列表
            DirectoryInfo[] floders = new DirectoryInfo(path).GetDirectories();
            // string[] floders = Directory.GetDirectories(path);
            foreach (DirectoryInfo diFloder in floders)
            {
                //获得文件名，创建列表对象
                //Expander exp = new Expander();
               // exp.IsExpanded = true;
                //stackPanel1.Children.Add(exp);
                //exp.Header = diFloder.Name;
               // ListBox listBox = new ListBox();
                lbxType.Width = 200;
                lbxType.IsSynchronizedWithCurrentItem = true;
                ScrollViewer.SetVerticalScrollBarVisibility(lbxType, ScrollBarVisibility.Disabled);
                lbxType.HorizontalAlignment = HorizontalAlignment.Left;
                ListBoxItem item = new ListBoxItem();
                item.Content = diFloder.Name;
                    item.Tag = diFloder.FullName;
                lbxType.Items.Add(item);
                item.Selected += new RoutedEventHandler(item_Selected);
              //  exp.Content = listBox;
                //获得子文件名称
                //DirectoryInfo dir = new DirectoryInfo(diFloder.FullName);
                //FileInfo[] chiFiles = dir.GetFiles();
                //foreach (FileInfo fl in chiFiles)
                //{
                //    ListBoxItem item = new ListBoxItem();
                //    item.Content = fl.Name.Substring(0, fl.Name.LastIndexOf('.'));
                //    item.Tag = fl.FullName;
                //    //添加点击事件
                //    item.Selected += new RoutedEventHandler(item_Selected);
                //    lbxType.Items.Add(item);
                //}
            }
            /**/
           // Expander expPublic = new Expander();
            //expPublic.IsExpanded = true;
            //stackPanel1.Children.Add(expPublic);
            //expPublic.Header = "通用控件";

           
        }
        ListBoxItem selectItem;
        string houseType;
        //选择类别
        void item_Selected(object sender, RoutedEventArgs e)
        {
            selectItem = sender as ListBoxItem;
            houseType = selectItem.Tag.ToString();
            if (houseType == "")
            {
                isEmpty = true;
            }
            else
            {
                isEmpty = false;
            }
            GetHouseInfo(houseType);
        }
        //获得每个类别下的户型
        void GetHouseInfo(string houseType)
        {
            lbxContent.Items.Clear();
            lbxContent.Width = 200;
            lbxContent.IsSynchronizedWithCurrentItem = true;
            ScrollViewer.SetVerticalScrollBarVisibility(lbxContent, ScrollBarVisibility.Disabled);
            lbxContent.HorizontalAlignment = HorizontalAlignment.Left;
            //产生空模板
            if (isEmpty)
            {
                ListBoxItem itemContent = new ListBoxItem();
                itemContent.Content = "空模板";
                itemContent.Tag = "";
                itemContent.Selected += new RoutedEventHandler(itemContent_Selected);
                lbxContent.Items.Add(itemContent);
                return;
            }
            //获得子文件名称
            if (!Directory.Exists(houseType))
            {
                MessageBox.Show(houseType+"不是文件夹");
                return;
            }
            DirectoryInfo dirTools = new DirectoryInfo(houseType);
            FileInfo[] files = dirTools.GetFiles("*.xml");
            foreach (FileInfo fl in files)
            {
                //获得文件名，创建列表对象
                ListBoxItem itemContent = new ListBoxItem();
                itemContent.Content = fl.Name.Substring(0, fl.Name.LastIndexOf('.'));
                itemContent.Tag = fl.FullName;
                itemContent.Selected += new RoutedEventHandler(itemContent_Selected);
                lbxContent.Items.Add(itemContent);
            }
        }
        //选择具体模板
        void itemContent_Selected(object sender, RoutedEventArgs e)
        {
            imgHouse.Source = null;
            if (isEmpty)
            {
                return;
            }
            selectItem = sender as ListBoxItem;
            string img=selectItem.Tag.ToString()+".png";
            if (!File.Exists(img))
            {
                MessageBox.Show("图片文件不存在");
                return;
            }
            BitmapImage image = new BitmapImage(new Uri(img, UriKind.Absolute));
            imgHouse.Source = image;
         
          
        }
             
    }
}
